package com.simtop.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.common.ServerResponse;
import com.simtop.pojo.User;
import com.simtop.pojo.wddata.Alarmre;
import com.simtop.pojo.wddata.AlarmreCord;
import com.simtop.pojo.wddata.MainTain;
import com.simtop.pojo.wddata.WdDate;
import com.simtop.service.AlarmreServer;
import com.simtop.service.ApiService;
import com.simtop.util.JwtUtil;
import com.simtop.vo.ApiVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

//设备数据
@RestController
@RequestMapping("/RustrDate")
@CrossOrigin
@Component
public class RustrDateController {

    @Autowired
    private ApiService apiService;

    @Autowired
    private AlarmreServer alarmreServer;

    //采样5分钟
    //@Scheduled(cron = "0 */5 * * * ?")


    @Scheduled(cron = "*/1 * * * * ?")
    public void scheduledRuster(){
        System.out.println("自动采集开启");
        Integer i = apiService.finOneTimeCount();
        //更新数据维护表所有的数据
        //查询表中所有数据 拿到id
        try {
            TimeUnit.SECONDS.sleep(i);//秒
        }
        catch (Exception e){
            e.printStackTrace();
        }
        //拿到最新的string
        ApiVo apiVo =  apiService.finnewData();
        //取出
        String allValue = apiVo.getAllValue();
        String plant = apiVo.getPlant();
        JSONObject jsonObject = JSON.parseObject(allValue);
        String rts = jsonObject.getString("rts");
        List<WdDate> lastDate = JSON.parseArray(rts, WdDate.class);
/*        for (WdDate as:lastDate
             ) {
            String tnm = as.getTnm();
            Float val = as.getVal();
            if(tnm.contains("IoStatus") && val == 0){
                String id = tnm.split("_")[1];
                String name = tnm.split("_")[0];

                System.out.println(id + name);

                for (WdDate as2:lastDate
                     ) {
                    if(as2.getTnm().endsWith(name+"_"+id+"_") && !as2.getTnm().endsWith("IoStatus") && !as2.getTnm().endsWith("KGJ")){
                        lastDate.remove(as2);
                    }


                }


            }

        }*/
        apiService.managedata(lastDate, plant);


        List<MainTain> findAlldeviceListAll= apiService.findAlldeviceListAll();
        for (MainTain mainTain:findAlldeviceListAll
             ) {


            int deviceId = mainTain.getDeviceId();
            String testType = mainTain.getTestType();
        //    System.out.println(deviceId);
         //   System.out.println(testType);
            //取到最新一条数据
            WdDate newWdDateValue = apiService.findDeviceValue(deviceId,testType);
            //设备最新值
            Float val = newWdDateValue.getVal();

            WdDate newWdDateStatus = apiService.findDeviceStatusValue(deviceId);
            float status = newWdDateStatus.getVal();

            //找到状态值
            int deviceStatusValue  = (int) status;

            //获取当前设备的时间
            Date devicetime = newWdDateStatus.getDevicetime();
            //操作 更新设备维护表

            mainTain.setDeviceValue(val);
            mainTain.setDeviceStatus(deviceStatusValue);
            mainTain.setSafeTime(devicetime);


        }

        //更新表操作
        apiService.updateDeviceList(findAlldeviceListAll);
        //更新表的定时任务结束

        //如果 当前的
        System.out.println("自动采集结束");



    }
    //走报警
    //@Scheduled(cron = "0/300 * * * * *")
    @Scheduled(cron = "0 */5 * * * ?")
    public void scheduledBj() {
        System.out.println("实时报警定时任务开启");
        //拿到每一个value值 预警范围值 一共三个
        List<MainTain> findAlldeviceListAll= apiService.findAlldeviceListAll();
        for (MainTain mainTain:findAlldeviceListAll
             ) {
            float deviceValue = mainTain.getDeviceValue();
            int deviceValue1 = (int) deviceValue;
            int devicePoliceup = mainTain.getDevicePoliceup();
            int devicePolicedown = mainTain.getDevicePolicedown();
            int deviceNormaldown1 = mainTain.getDeviceNormaldown();
            int deviceNormalup1 = mainTain.getDeviceNormalup();
            int deviceStatus = mainTain.getDeviceStatus();
            int policeControl = mainTain.getPoliceControl();
            String testType1 = mainTain.getTestType();
            int deviceId = mainTain.getDeviceId();
            String deviceName1 = mainTain.getDeviceName();
            int yjControl = mainTain.getYjControl();

            // 0 关闭 报警
            if(policeControl == 0){
                //删除 数据
              //  alarmreServer.deletename(deviceName1);
                continue;
            }


            //判断是否 连接异常
            if((deviceStatus == 0  && testType1.equals("温度")) || (deviceStatus == 0 && testType1.equals("风速"))
                    || (deviceStatus == 0 && testType1.equals("压差")) || (deviceStatus == 0 && testType1.equals("一万颗粒度")) ){
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                String timenew = df.format(mainTain.getSafeTime());
                //如果 有 一条  未处置的  id 和状态 都 一样的 就不加入了
                int sa = alarmreServer.getOne(deviceId,timenew);
/*                if(sa > 0){
                    break;
                }*/

                //产生报警记录加入表中
                // 报警类别 超出阈值
                //表中加一条报警信息
                AlarmreCord alarmreCord = new AlarmreCord();

                alarmreCord.setAlarmType("连接异常");
                alarmreCord.setAlarmTime(mainTain.getSafeTime());
                alarmreCord.setStringTime(df.format(mainTain.getSafeTime()));
                alarmreCord.setDevicepoliceFor(mainTain.getDevicepoliceFor());
                //设备名称
                String deviceName = mainTain.getDeviceName();
                //设备值
                float deviceValue2 = mainTain.getDeviceValue();
                //类型值
                String testType = mainTain.getTestType();
                //正常值 下限
                int deviceNormaldown = mainTain.getDeviceNormaldown();
                //正常值 上限
                int deviceNormalup = mainTain.getDeviceNormalup();
                //预警值 下限
                int devicePolicedown1 = mainTain.getDevicePolicedown();
                //预警值上限
                int devicePoliceup1 = mainTain.getDevicePoliceup();
                //单位
                String deviceMark = mainTain.getDeviceMark();
                alarmreCord.setAlarmMessage(deviceName+",连接失败");
                alarmreCord.setStatus(1);
                alarmreCord.setDeviceName(deviceName);
                alarmreCord.setDeviceId(mainTain.getDeviceId());
                alarmreCord.setAlarmManage(0);
                alarmreCord.setDeviceType(testType);
                SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
                alarmreCord.setLastTime(df2.format(mainTain.getSafeTime()));
                alarmreServer.insertOneAlarmer(alarmreCord);

                System.out.println("新增一条连接异常实时报警信息");

                continue;


            }


            //判断是否 数据异常
            if(deviceValue1 == 0 && deviceStatus != 0){
                //产生报警记录加入表中
                // 报警类别 超出阈值
                //表中加一条报警信息
                AlarmreCord alarmreCord = new AlarmreCord();
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                alarmreCord.setAlarmType("数据异常");
                alarmreCord.setAlarmTime(mainTain.getSafeTime());
                alarmreCord.setStringTime(df.format(mainTain.getSafeTime()));
                alarmreCord.setDevicepoliceThree(mainTain.getDevicepoliceThree());
                //设备名称
                String deviceName = mainTain.getDeviceName();
                //设备值
                float deviceValue2 = mainTain.getDeviceValue();
                //类型值
                String testType = mainTain.getTestType();
                //正常值 下限
                int deviceNormaldown = mainTain.getDeviceNormaldown();
                //正常值 上限
                int deviceNormalup = mainTain.getDeviceNormalup();
                //预警值 下限
                int devicePolicedown1 = mainTain.getDevicePolicedown();
                //预警值上限
                int devicePoliceup1 = mainTain.getDevicePoliceup();
                //单位
                String deviceMark = mainTain.getDeviceMark();
                alarmreCord.setAlarmMessage(deviceName+",无法正常获取该设备数据");
                alarmreCord.setStatus(1);
                alarmreCord.setDeviceName(deviceName);
                alarmreCord.setDeviceId(mainTain.getDeviceId());
                alarmreCord.setAlarmManage(0);
                alarmreCord.setDeviceType(testType);
                SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
                alarmreCord.setLastTime(df2.format(mainTain.getSafeTime()));
                alarmreServer.insertOneAlarmer(alarmreCord);

                System.out.println("新增一条数据异常实时报警信息");

                continue;
            }




            // 0 关闭 报警
            if(yjControl == 0){
                //删除 数据
              //  alarmreServer.deletename2(deviceName1);
                continue;
            }

            //判断是否 超出预警
            if((devicePolicedown>deviceValue1 & deviceValue1>deviceNormaldown1 && deviceStatus != 0 && deviceValue1 != 0) || (deviceValue1>devicePoliceup & deviceValue1<deviceNormalup1) && deviceStatus != 0 && deviceValue1 != 0){
                //产生报警记录加入表中
                // 报警类别 超出阈值
                //表中加一条报警信息
                AlarmreCord alarmreCord = new AlarmreCord();
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                alarmreCord.setAlarmType("超出预警");
                alarmreCord.setAlarmTime(mainTain.getSafeTime());
                alarmreCord.setStringTime(df.format(mainTain.getSafeTime()));
                alarmreCord.setDevicepoliceTwo(mainTain.getDevicepoliceTwo());
                //设备名称
                String deviceName = mainTain.getDeviceName();
                //设备值
                float deviceValue2 = mainTain.getDeviceValue();
                //类型值
                String testType = mainTain.getTestType();
                //正常值 下限
                int deviceNormaldown = mainTain.getDeviceNormaldown();
                //正常值 上限
                int deviceNormalup = mainTain.getDeviceNormalup();
                //预警值 下限
                int devicePolicedown1 = mainTain.getDevicePolicedown();
                //预警值上限
                int devicePoliceup1 = mainTain.getDevicePoliceup();
                //单位
                String deviceMark = mainTain.getDeviceMark();
                alarmreCord.setAlarmMessage(deviceName+",检测值超标,实时检测值为"+deviceValue2+
                        deviceMark+",正常范围("+deviceNormaldown+deviceMark+"-"+deviceNormalup+deviceMark+
                        "),预警范围("+devicePolicedown1+deviceMark+"-"+devicePoliceup1+deviceMark+")");
                alarmreCord.setStatus(1);
                alarmreCord.setDeviceName(deviceName);
                alarmreCord.setDeviceId(mainTain.getDeviceId());
                alarmreCord.setAlarmManage(0);
                alarmreCord.setDeviceType(testType);
                SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
                alarmreCord.setLastTime(df2.format(mainTain.getSafeTime()));
                alarmreServer.insertOneAlarmer(alarmreCord);

                System.out.println("新增一条超出预警实时报警信息");

                continue;
            }

            //判断是否 超出阈值
            if((deviceNormaldown1>deviceValue1 && deviceStatus != 0 && deviceValue1 != 0) || (deviceValue1>deviceNormalup1 && deviceStatus != 0 && deviceValue1 != 0) ){
                //产生报警记录加入表中
                // 报警类别 超出阈值
                //表中加一条报警信息
                AlarmreCord alarmreCord = new AlarmreCord();
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                alarmreCord.setAlarmType("超出阈值");
                alarmreCord.setAlarmTime(mainTain.getSafeTime());
                alarmreCord.setStringTime(df.format(mainTain.getSafeTime()));
                alarmreCord.setDevicepoliceOne(mainTain.getDevicepoliceOne());
                //设备名称
                String deviceName = mainTain.getDeviceName();
                //设备值
                float deviceValue2 = mainTain.getDeviceValue();
                //类型值
                String testType = mainTain.getTestType();
                //正常值 下限
                int deviceNormaldown = mainTain.getDeviceNormaldown();
                //正常值 上限
                int deviceNormalup = mainTain.getDeviceNormalup();
                //预警值 下限
                int devicePolicedown1 = mainTain.getDevicePolicedown();
                //预警值上限
                int devicePoliceup1 = mainTain.getDevicePoliceup();
                //单位
                String deviceMark = mainTain.getDeviceMark();
                alarmreCord.setAlarmMessage(deviceName+",检测值超标,实时检测值为"+deviceValue2+
                        deviceMark+",正常范围("+deviceNormaldown+deviceMark+"-"+deviceNormalup+deviceMark+
                        "),预警范围("+devicePolicedown1+deviceMark+"-"+devicePoliceup1+deviceMark+")");
                alarmreCord.setStatus(1);
                alarmreCord.setDeviceName(deviceName);
                alarmreCord.setDeviceId(mainTain.getDeviceId());
                alarmreCord.setAlarmManage(0);
                alarmreCord.setDeviceType(testType);
                SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
                alarmreCord.setLastTime(df2.format(mainTain.getSafeTime()));
                alarmreServer.insertOneAlarmer(alarmreCord);

                System.out.println("新增一条超出阈值实时报警信息");

                continue;
            }


            System.out.println("实时报警定时任务结束");

        }

    /*    System.out.println("同步报警唯一记录开启");
        List<MainTain> findAlldeviceListAll2= apiService.findAlldeviceListAll();
        //拿到id 和 type
        for (MainTain devicelist:findAlldeviceListAll2
        ) {
            int deviceId = devicelist.getDeviceId();
            String testType = devicelist.getTestType();
            //先找到有没有属于id 和 type 的 记录
            int  deviceSize = alarmreServer.findByidandtype(deviceId,testType);
            //     System.out.println(deviceSize);
            //如果== 0 条 才可以新增唯一报警记录

            //通过id 和 type 拿到最新的一条数据
            AlarmreCord alarmerCord=alarmreServer.finNewDeviceAlarmre(deviceId,testType);

            //
            if(alarmerCord == null){
                //报警信息总表没有数据
                continue;

            }else {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                alarmerCord.setStringTime(df.format(alarmerCord.getAlarmTime()));

                SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
                alarmerCord.setLastTime(df2.format(alarmerCord.getAlarmTime()));


                //插入唯一报警表
                alarmreServer.insertOnlyOneAlarmre(alarmerCord);
                //插入一条 推送u3d一次
                System.out.println("插入唯一报警表");

            }






        }

*/




    }

    //五分钟同步一次唯一报警记录
    //@Scheduled(cron = "0/300 * * * * *")
/*    @Scheduled(cron = "0/300 * * * * *")
    public void scheduledBjSy() {

    }*/
    }




